package com.hoppinzq.test;

import org.htmlunit.BrowserVersion;
import org.htmlunit.WebClient;
import org.htmlunit.corejs.javascript.Node;
import org.htmlunit.html.*;
import org.htmlunit.javascript.host.html.HTMLDivElement;
import org.htmlunit.javascript.host.html.HTMLLinkElement;
import org.htmlunit.javascript.host.html.HTMLUListElement;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhangQi
 */
public class demo6 {

    @Test
    public void wyBuff_Dota2() throws IOException {
        //
        //
        final WebClient webClient = new WebClient();
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setTimeout(5000);
        final HtmlPage page = webClient.getPage("https://buff.163.com/market/dota2#tab=selling&page_num=1");
        final HtmlUnorderedList htmluListElement = (HtmlUnorderedList) page.getByXPath("*[@id=\"j_list_card\"]/ul").get(0);
        List listItems = htmluListElement.getAncestors();
        int i = 0;
    }

    @Test
    public void buff_main() throws IOException {
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setCssEnabled(false);
        webClient.waitForBackgroundJavaScript(10*1000);
        String url = "https://buff.163.com/?game=dota2";
        HtmlPage page = webClient.getPage(url);
        List<HtmlElement> goodsList = page.getByXPath("//*[@id=\"index-popular_goods-data-list-dota2\"]/li");
        for (HtmlElement goods : goodsList) {
            String name = goods.getElementsByTagName("h3").get(0).getTextContent();
            String image = goods.getElementsByTagName("a").get(0).getElementsByTagName("img").get(0).getAttribute("data-original");
            String price = goods.getElementsByTagName("p").get(1)
                    .getElementsByTagName("strong").get(0)
                    .getElementsByTagName("span").get(0).getAttribute("data-price");
            System.err.println(name + " " +image+" "+ price);
        }
    }

    public String getPageWaitJS (String url) throws IOException {
        WebClient webClient = new WebClient();
        webClient.getOptions().setJavaScriptEnabled(true); //启用JS解释器，默认为true
        webClient.getOptions().setCssEnabled(false); //禁用css支持
        webClient.getOptions().setThrowExceptionOnScriptError(false); //js运行错误时，是否抛出异常
        HtmlPage page = webClient.getPage(url);
        webClient.waitForBackgroundJavaScript(3*1000);
        String pageXml = page.asXml(); //以xml的形式获取响应文本
        return pageXml;
    }

    @Test
    public void buff_login() throws IOException {
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setCssEnabled(false);
        webClient.waitForBackgroundJavaScript(10*1000);
        String url = "https://buff.163.com/?game=dota2";
        final HtmlPage page1 = webClient.getPage(url);
        //10天内免登录
        DomElement checkBoxs1 = page1.getHtmlElementById("remember-me");
        if(checkBoxs1!=null){
            HtmlDivision checkBox_remember= (HtmlDivision) checkBoxs1;
            checkBox_remember.click();
        }
        //我同意
        DomElement checkBoxs2 = page1.getHtmlElementById("agree-checkbox");
        if(checkBoxs2!=null){
            HtmlDivision checkBox_agree= (HtmlDivision) checkBoxs2;
            checkBox_agree.click();
        }
        //手机号
        DomElement input_mobile = page1.getHtmlElementById("phoneipt");
        if(input_mobile!=null){
            HtmlTextInput input_phone= (HtmlTextInput) input_mobile;
            input_phone.setText("15028582175");
            System.err.println("手机号输入："+input_phone.getText());
        }

        //密码
        List<DomElement> input1 = page1.getByXPath("/html/body/div[2]/div[2]/div[2]/form/div/div[4]/div/div[1]/div[1]/input");
        if(input1.size()!=0){
            HtmlTextInput input_password= (HtmlTextInput) input1.get(0);
            input_password.setText("19zhangqi");
            System.err.println("密码输入："+input_password.getText());
        }

        //登录
        List<DomElement> a1 = page1.getByXPath("/html/body/div[2]/div[2]/div[2]/form/div/div[7]/a");
        if(a1.size()!=0){
            HtmlAnchor a_login= (HtmlAnchor) a1.get(0);
            //a_login.click();
            System.err.println("按钮；"+a_login.asXml());
        }


    }

}

